package com.ruoyi.basice_info.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.basice_info.domain.TsExamSyllabusVo;
import com.ruoyi.basice_info.mapper.TbSemesterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basice_info.mapper.TbAcademicYearMapper;
import com.ruoyi.basice_info.domain.TbAcademicYear;
import com.ruoyi.basice_info.service.ITbAcademicYearService;

/**
 * 学年设置Service业务层处理
 *
 * @author hongyi
 * @date 2023-12-07
 */
@Service
public class TbAcademicYearServiceImpl implements ITbAcademicYearService
{
    @Autowired
    private TbAcademicYearMapper tbAcademicYearMapper;

    @Autowired
    private TbSemesterMapper tbSemesterMapper; // 这里面包含了考试大纲
    /**
     * 查询学年设置
     *
     * @param academicYearId 学年设置主键
     * @return 学年设置
     */
    @Override
    public TbAcademicYear selectTbAcademicYearByAcademicYearId(Long academicYearId)
    {
        return tbAcademicYearMapper.selectTbAcademicYearByAcademicYearId(academicYearId);
    }

    @Override
    public TbAcademicYear getTbAcademicYearByCode(String code) {
        return tbAcademicYearMapper.getTbAcademicYearByCode(code);
    }

    @Override
    public TbAcademicYear getTbAcademicYearByName(String name) {
        return tbAcademicYearMapper.getTbAcademicYearByName(name);
    }


    /**
     * 查询学年设置列表
     *
     * @param tbAcademicYear 学年设置
     * @return 学年设置
     */
    @Override
    public List<TbAcademicYear> selectTbAcademicYearList(TbAcademicYear tbAcademicYear)
    {
        return tbAcademicYearMapper.selectTbAcademicYearList(tbAcademicYear);
    }

    /**
     * 新增学年设置
     *
     * @param tbAcademicYear 学年设置
     * @return 结果
     */
    @Override
    public int insertTbAcademicYear(TbAcademicYear tbAcademicYear)
    {
        return tbAcademicYearMapper.insertTbAcademicYear(tbAcademicYear);
    }

    /**
     * 修改学年设置
     *
     * @param tbAcademicYear 学年设置
     * @return 结果
     */
    @Override
    public int updateTbAcademicYear(TbAcademicYear tbAcademicYear)
    {
        return tbAcademicYearMapper.updateTbAcademicYear(tbAcademicYear);
    }

    /**
     * 批量删除学年设置
     *
     * @param academicYearIds 需要删除的学年设置主键
     * @return 结果
     */
    @Override
    public int deleteTbAcademicYearByAcademicYearIds(Long[] academicYearIds)
    {
        int num = -1;
        int nums = 0;
        List<Long> longList = Arrays.asList(academicYearIds);
        for (Long item: longList) {
            TsExamSyllabusVo syllabus = new TsExamSyllabusVo();
            syllabus.setAcademicYearId(item);
            // 去子表中查询一下
            List<TsExamSyllabusVo> syllabusList = tbSemesterMapper.selectTsExamSyllabusList(syllabus);
            // 判断一下是否有有元素
            if(syllabusList.size()<=0) {
                // 在子表中不存在则删除
                num = tbAcademicYearMapper.deleteTbAcademicYearByAcademicYearId(item);
            } else {
                nums ++;
            }
        }
        if(longList.size() == 1) {
            if(nums>0) {
                throw new IllegalArgumentException("该数据在考试大纲设置中使用，不能删除");
            } else  {
                if (nums > 0) {
                    throw new IllegalArgumentException("该数据在考试大纲设置中使用，不能删除");
                }
            }
        }
//        return tbSemesterMapper.deleteTbSemesterBySemesterIds(semesterIds);
        return num;
//        return tbAcademicYearMapper.deleteTbAcademicYearByAcademicYearIds(academicYearIds);
    }

    /**
     * 删除学年设置信息
     *
     * @param academicYearId 学年设置主键
     * @return 结果
     */
    @Override
    public int deleteTbAcademicYearByAcademicYearId(Long academicYearId)
    {
        return tbAcademicYearMapper.deleteTbAcademicYearByAcademicYearId(academicYearId);
    }
}
